#include "log4cpp/Category.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/RollingFileAppender.hh" //回卷文件
#include "log4cpp/PatternLayout.hh" //设置日志布局的头文件
#include<iostream>
#include<string>
#include<func.h>
using std::cout;
using std::endl;
using std::string;
using namespace log4cpp;
#define addprefix(msg) string("[").append(__FILE__)\
            .append(":").append(__func__)\
            .append(":").append(std::to_string(__LINE__))\
            .append("]").append(msg).c_str()
class Mylogger
{
public:
	void warn(const char *msg);
	void error(const char *msg);
	void debug(const char *msg);
	void info(const char *msg);
private:
    static log4cpp::Category* myCategory(){
        if(myTestDepart==nullptr){
            PatternLayout *pt1 = new PatternLayout();
            pt1->setConversionPattern("%d %c [%p] %m %n");
            OstreamAppender *pos = new OstreamAppender("consle",&cout);
            pos->setLayout(pt1);
            Category& myTest = Category::getInstance("myTest");
            myTest.setPriority(Priority::WARN);
            myTest.addAppender(pos);
            //cout<<"this is Category* myCategory()"<<endl;
            return &myTest;
        }
        return myTestDepart;
    }
public:
    static Mylogger*getInstance(){
        if(mylogger == nullptr){
            mylogger = new Mylogger();
            mylogger->myTestDepart = myCategory();
        }
        return mylogger;
    }
	Mylogger& operator=(const Mylogger&rhs) = delete;
    Mylogger(const Mylogger&rhs) = delete;
private:
	Mylogger()
    {
    }
	~Mylogger(){
        if(myTestDepart){
            Category::shutdown();
            delete myTestDepart;
        }
    }
    
private:
    static log4cpp::Category *myTestDepart;
    static Mylogger* mylogger;
};
Mylogger* Mylogger::mylogger = nullptr;
log4cpp::Category * Mylogger::myTestDepart = nullptr;

void Mylogger::error(const char *msg){
    myCategory()->error(msg); 
    //cout<<"this is Mylogger::error"<<endl;
}
void Mylogger::debug(const char *msg){

    myCategory()->debug(msg); 
    //cout<<"this is Mylogger::debug"<<endl;    
}
void Mylogger::info(const char *msg){
    myCategory()->info(msg); 
    //cout<<"this is Mylogger::info"<<endl;    
}
void Mylogger::warn(const char *msg){
    myCategory()->warn(msg); 
}

inline
const char* pirntMsg(const char*msg){
    printf("\033[34;1;47m %s \033[0m",msg);
    return msg;
}

void test0()
{
    //第一步，完成单例模式的写法
    Mylogger *log = Mylogger::getInstance();
    log->warn("this is warn msg");
    //log->error("this is error msg");
    //log->debug("this is debug msg");
    //log->info("this is info");
    
    log->warn(pirntMsg(addprefix("this is warn msg")));
}
void LogInfo(const char*pstr){
    Mylogger::getInstance()->info(pstr);
}
void LogError(const char*pstr){
    Mylogger::getInstance()->error(pstr);
}
void LogDebug(const char*pstr){
    Mylogger::getInstance()->debug(pstr);
}
void LogWarn(const char*pstr){
    Mylogger::getInstance()->warn(pstr);
}
void test1() 
{
    printf("hello,world\n");
    LogInfo("The log is info message");
    LogError("The log is error message");
    LogWarn("The log is warn message");
    LogDebug("The log is debug message");
}
void test3()
{
    Mylogger*log1 = Mylogger::getInstance();
    Mylogger*log2 = Mylogger::getInstance();
    printf("%p\n",log1);
    printf("%p\n",log2);
}



int main(void){
    test0();
    //test3();
    return 0;
}